package com.yan.utils;

import java.text.MessageFormat;

import org.apache.log4j.Logger;

public class LogUtil {

    static Logger logger = Logger.getLogger(LogUtil.class);

    public static String getStackMessage(Exception e) {
        StringBuilder error = new StringBuilder();
        StackTraceElement[] stacks = e.getStackTrace();
        for (StackTraceElement stack : stacks) {
            String className = stack.getClassName();
            StringBuffer buffer = new StringBuffer();
            buffer.append("className:" + className);
            buffer.append("\t method:" + stack.getMethodName());
            buffer.append("\t line:" + stack.getLineNumber());
            error.append(buffer.toString());
        }
        System.out.println(error.toString());
        return error.toString();
    }

    /**
     * 打印log
     *
     * @param log
     * @param arguments
     */
    public static void printInfo(Logger logg, String log, Object... arguments) {
        logg.info(logMsg(log, arguments));
    }

    /**
     * @category 打印带异常的日志
     */
    public static void printInfo(Logger logg, Exception exception, String log, Object... arguments) {
        logg.info(logMsg(log, arguments), exception);
    }

    /**
     * 打印log
     */
    public static void printError(Logger logg, String log, Object... arguments) {
        logg.error(logMsg(log, arguments));
    }

    /**
     * @category 打印带异常的错误日志
     */
    public static void printError(Logger logg, Exception exception, String log, Object... arguments) {
        logg.error(logMsg(log, arguments), exception);
    }

    /**
     * 打印log
     */
    public static void printWarn(Logger logg, String log, Object... arguments) {
        logg.warn(logMsg(log, arguments));
    }

    /**
     * @param logg
     * @param exception
     * @param log
     * @param arguments
     * @category 打印带异常的警告日志
     * @author Yue Chang
     * @date 2017年6月3日 下午3:09:01
     * @since 个人中心
     */
    public static void printWarn(Logger logg, Exception exception, String log, Object... arguments) {
        logg.warn(logMsg(log, arguments), exception);
    }

    private static String logMsg(String log, Object... arguments) {
        String message = MessageFormat.format(log, arguments);
        return message;
    }


}
